package com.itheima79.googleplay.holder;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.itheima79.googleplay.R;
import com.itheima79.googleplay.bean.AppInfo;
import com.itheima79.googleplay.global.GooglePlayApplication;
import com.itheima79.googleplay.global.ImageLoaderOptions;
import com.itheima79.googleplay.http.Api;
import com.itheima79.googleplay.ui.activity.ImageScaleActivity;
import com.itheima79.googleplay.util.CommonUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

public class AppScreenHolder extends BaseHolder<AppInfo>{
	private LinearLayout ll_screen;
	private Activity activity;
	@Override
	public View initHolderView() {
		View view = View.inflate(GooglePlayApplication.context, R.layout.layout_detail_app_screen, null);
		ll_screen = (LinearLayout) view.findViewById(R.id.ll_screen);
		return view;
	}
	/**
	 * 绑定Activity
	 */
	public void attachActivity(Activity activity){
		this.activity = activity;
	}
	
	@Override
	public void bindData(AppInfo data) {
		int width = CommonUtil.getDimens(R.dimen.dp90);
		int height = CommonUtil.getDimens(R.dimen.dp150);
		int margin = CommonUtil.getDimens(R.dimen.dp8);
				
		final ArrayList<String> screen = data.getScreen();
		for (int i = 0; i < screen.size(); i++) {
			ImageView imageView = new ImageView(GooglePlayApplication.context);
			LayoutParams params = new LayoutParams(width, height);
			params.leftMargin = (i==0?0:margin);
			imageView.setLayoutParams(params);
			//显示图片
			ImageLoader.getInstance().displayImage(Api.IMAGE_PREFIX+screen.get(i), imageView,ImageLoaderOptions.fadein_options);
			
			//将ImageView加入进来
			ll_screen.addView(imageView);
			
			final int temp = i;
			//添加点击事件
			imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//开启缩放大图的界面
//					Intent intent = new Intent(GooglePlayApplication.context,ImageScaleActivity.class);
//					//如果使用不是Activity的Context来开启Activity，那么需要该标记
//					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					intent.putStringArrayListExtra("urlList", screen);
//					GooglePlayApplication.context.startActivity(intent);
					
					//第二种方案，使用Activity来开启
					Intent intent = new Intent(activity,ImageScaleActivity.class);
					intent.putStringArrayListExtra("urlList", screen);
					intent.putExtra("currentItem",temp);
					activity.startActivity(intent);
				}
			});
		}
	}

}
